﻿
#region NameSpace
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using UserManage;
using Eb.MessageBox;
#endregion

public partial class ucAddRole : System.Web.UI.UserControl
{
    #region Fields
    UserManager _userRole = new UserManager();
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        btnBack.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnBack.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnSaveRole.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSaveRole.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");

        if (!IsPostBack)
        {
            if (Session["UserManager_Role"] == "Role Edit")
                btnSaveRole.Text = "Update";
            if (Session["UserManager_Role"].ToString() == "Role Edit")
                LoadData();
        }
    } 
  
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucRole.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }

    protected void btnSaveRole_Click(object sender, EventArgs e)
    {
        //if (txtRoleName.Text == string.Empty)
        //{
        //    Response.Write("<script>alert('Enter Role Name')</script>");
        //    return;
        //}
        if (Session["UserManager_Role"].ToString() == "Role Add")
            Add();
        else
            Edit();

        Clear();
    }
    #endregion

    #region Methods
    void Add()
    {
        try
        {
            string roleName = txtRoleName.Text;
            string description = txtDescription.Text;
            if (!_userRole.AddRole(roleName, description))
            {
                MessageBox.Show("Role Already Exist!");
                //Response.Write("<script>alert('Role Already Exist')</script>");
            }
            else
                MessageBox.Show("Successfully Added");
                //Response.Write("<script>alert('Added Successfully !')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Add Role Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void Edit()
    {
        try
        {
            UserRoles editRole = new UserRoles();
            editRole.RoleID = Session["UserManager_RoleID"].ToString();
            editRole.RoleName= txtRoleName.Text;
            editRole.RoleDescription=txtDescription.Text;
            _userRole.RoleEdit(editRole);
            MessageBox.Show("Succesfully Updated");
                //Response.Write("<script>alert('Updated Successfully !')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Add Role Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void Clear()
    {
        txtRoleName.Text = string.Empty;
        txtDescription.Text = string.Empty;
    }

    void LoadData()
    {
        string condition = "UR_RoleID = " + Session["UserManager_RoleID"] + " ";
        DataSet dsUser = _userRole.GetData("UserRolesList", condition);

        txtRoleName.Text = dsUser.Tables[0].Rows[0]["UR_RoleName"].ToString();
        txtDescription.Text = dsUser.Tables[0].Rows[0]["UR_RoleDescription"].ToString();
    }
    #endregion
}
